<?php
class ControllerAction extends Zend_Controller_Action
{
	protected $_bootstrap;
	
	protected $_schema;
	
	protected $_table;
	
	protected $_cms;
	
	public function getBootstrap() {
		if($this->_bootstrap == null) {
			$this->setBootstrap();
		}
		
		return $this->_bootstrap;
	}
	
	public function setBootstrap() {
		$this->_bootstrap = $this->getInvokeArg('bootstrap');
		return $this;
	}
	
	public function setCms()
	{
		$params = $this->getRequest()->getParams();
		$params['schema'] = $this->getSchema();
		
		$this->_cms = Cms::factory(
			$this->getCrudAction(),
			isset($params['t']) ? $params['t']: null,
			$params['schema'],
			null // Database
		);
		
		$this->view->table = array(
			'name' => $this->getTable(),
			'alias' => $this->getCms()->getAdapter()->rename($this->getTable())
		);
		
		return $this;
	}
	
	public function getCms()
	{
		return $this->_cms;
	}
	
	public function setSchema($schema)
	{
		$this->_schema = $schema;
		return $this;
	}
	
	public function getSchema()
	{
		return $this->_schema;
	}
	
	public function setTable($table)
	{
		$this->_table = $table;
		return $this;
	}
	
	public function getTable()
	{
		return $this->_table;
	}
	
	public function getCrudAction()
	{
		$actions = array(
			'index' => 'Read',
			'list' => 'Read',
			'detail' => 'Read',
			'edit' => 'CreateUpdate',
			'new' => 'CreateUpdate',
			'delete' => 'Delete'
		);
		
		$action = $this->getRequest()->getActionName();
		
		if(!isset($actions[$action])) {
			return 'read';
		}
		
		return $actions[$action];
	}
}